#include "player.h"
#include "gameboardorganizer.h"
#include "gamedialogmanager.h"
#include "tileboarditems.h"
#include "playerskillobject.h"
#include <QDebug>
#include "qmath.h"
/*

  PLAYER CLASS

*/
Player::Player(QObject *parent) :
    QObject(parent)
{
    setDead(false);
    setDefaultItem();
}

void Player::setDefaultItem(){
    _resistR = 0;
    _resistG = 0;
    _resistY = 0;
    _resistBlue = 0;
    _resistBlack = 0;
    _buddhaNum = 0;
    _skillSealed = false;
    _actionUsed = false;

    if(!_isDead){
        _pos = new QPoint(1,1);
        _hasYinyang =true;
        _qiNum = 3;
    }
}

void Player::setPlayerPos(QPoint *p){
    _moveDir = QPoint((p->x()-_pos->x()),(p->y()-_pos->y()));
    _pos = p;
    emit posChanged();
}

QPoint* Player::getMoveDistance(QPoint* nPos){
    int disX = nPos->x() - _pos->x();
    int disY = nPos->y() - _pos->y();
    return new QPoint(disX,disY);
}

void Player::qiChange(bool isIncease){
    int current_qi = getQi();
    if(isIncease)
        setQi(current_qi+1);
    else{
        if(current_qi>0) //make sure the number is above 0
            setQi(current_qi-1);
    }
}

void Player::buddaChange(bool isIncese){
    int currentBudda = getBudd();
    if(isIncese){
        if(currentBudda<2)
            setBudda(currentBudda+1);
    }
    else{
        if(currentBudda>0)
            setBudda(currentBudda-1);
    }
}

void Player::checkDead(){
    int current_qi = getQi();
    if(current_qi>0)
        setDead(false);
    else{
        setDead(true);
        setDefaultItem();
    }
}

void Player::resistChangeR(bool isIncease){
    int currentR = getResistR();
    if(isIncease){
        if(currentR<4)
            setResistR(currentR+1);
    }
    else{
        if(currentR>0)
            setResistR(currentR-1);
    }
    emit taoChanged();
}

void Player::resistChangeG(bool isIncease){
    int currentG = getResistG();
    if(isIncease){
        if(currentG<4)
            setResistG(currentG+1);
    }
    else{
        if(currentG>0)
            setResistR(currentG-1);
    }
    emit taoChanged();
}

void Player::resistChangeY(bool isIncease){
    int currentY = getResistY();
    if(isIncease){
        if(currentY<4)
            setResistY(currentY+1);
    }
    else{
        if(currentY>0)
            setResistY(currentY-1);
    }
    emit taoChanged();
}

void Player::resistChangeBlue(bool isIncease){
    int currentBlue = getResistBlue();
    if(isIncease){
        if(currentBlue<4)
            setResistBlue(currentBlue+1);
    }
    else{
        if(currentBlue>0)
            setResistBlue(currentBlue-1);
    }
    emit taoChanged();
}

void Player::resistChangeBlack(bool isIncease){
    int currentBlack = getResistBlack();
    if(isIncease){
        if(currentBlack<4)
            setResistBlack(currentBlack+1);
    }
    else{
        if(currentBlack>0)
            setResistBlack(currentBlack-1);
    }
    emit taoChanged();
}

Player* Player::receiveBoard(GhostBoard *board){
    _board = board;
    board->setPlayer(this);
    return this;
}

void Player::resetAllResists()
{
    _resistR = 0;
    _resistBlue = 0;
    _resistY = 0;
    _resistG = 0;
    _resistBlack = 0;
}


/*
  PLAYER ICON CLASS
*/

PlayerIcon::PlayerIcon(GameBoardOrganizer *parent, QPixmap p, Player *pl)
    :GameBoardItem(parent), _iconP(p),_pl(pl)
{
    _iconW = p.width();
    _iconH = p.height();

}

QRectF PlayerIcon::boundingRect() const
{
    return QRectF(-_iconW/2,-_iconH/2,_iconW,_iconH);
}

void PlayerIcon::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    painter->drawPixmap(-_iconW/2,-_iconH/2,_iconW,_iconH,_iconP);
}

void PlayerIcon::updateIconPos()
{
    int moveDis = VillageTile::WIDTH;
    QPoint direct = _pl->getMoveDirection();
    this->moveBy(moveDis*(direct.x()),moveDis*(direct.y()));
    update();

}


